
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.entity.UserCenterBannerInfo;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:获取用户中心Banner信息请求类
 *
 * @author jamywang
 * @since 2016年5月9日 下午3:32:20
 */
public class RequestUserCenterBannerTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public UserCenterBannerResponse request() {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> params = new HashMap<String, Object>();
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        ResponsePackage responsePackage = new UserCenterBannerResponsePackage();
        UserCenterBannerResponse responseObj = new UserCenterBannerResponse();
        params.put("cmd", ProtocolCmd.CMD_USER_CENTER_BANNER);
        requestParams.add(params);
        requestPackage.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_USER_CENTER_BANNER);
        try {
            KGHttpClient.request(requestPackage, responsePackage);
            responsePackage.getResponseData(responseObj);
            return responseObj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    class UserCenterBannerResponsePackage extends HttpBaseResponsePackage<UserCenterBannerResponse> {

        @Override
        protected void handleResponse(UserCenterBannerResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "RequestUserCenterBannerTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i > mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.getString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_USER_CENTER_BANNER.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            JSONObject obj = jsonResult.optJSONObject("target");
                            UserCenterBannerInfo bannerInfo = new UserCenterBannerInfo();
                            bannerInfo.setImageUrlPortrait(jsonResult.optString("imageurl"));
                            bannerInfo.setImageUrlLandscape(jsonResult.optString("imageurl2"));
                            bannerInfo.setDecs(jsonResult.optString("desc"));
                            if (obj != null) {
                                bannerInfo.setType(obj.optInt("type"));
                                bannerInfo.setTitle(obj.optString("title"));
                                bannerInfo.setUrl(obj.optString("url"));
                            }
                            response.setUserCenterBannerInfo(bannerInfo);
                        }
                    }
                    response.setOk(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class UserCenterBannerResponse extends ResponseMessage {

        UserCenterBannerInfo userCenterBannerInfo;

        public UserCenterBannerInfo getUserCenterBannerInfo() {
            return userCenterBannerInfo;
        }

        public void setUserCenterBannerInfo(UserCenterBannerInfo userCenterBannerInfo) {
            this.userCenterBannerInfo = userCenterBannerInfo;
        }

    }

}
